﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Framework;

using ESRI.ArcGIS.ADF.BaseClasses;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 修复数据源
    /// </summary>
    public class RepareDataSource : BaseCommand
    {
        /// <summary>
        /// 应用程序
        /// </summary>
        IApplication _Application = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public RepareDataSource()
        {
            base.m_caption = "修复数据源";
            base.m_toolTip = base.Caption;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (_Application == null)
                _Application = hook as IApplication;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnClick()
        {
            if (_Application == null) return;
            if (!((this._Application as IFramework).TOCControl.CustomProperty is ILayer)) return;
            OpenDataDialog dlg = new OpenDataDialog();
            dlg.MultiSelect = false;
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            List<IDataset> selLyrs = dlg.GetSelectDatasets();
            if (selLyrs == null || selLyrs.Count == 0) return;
            ILayer layer = (this._Application as IFramework).TOCControl.CustomProperty as ILayer;
            if (layer is IFeatureLayer)
            {
                IFeatureClass fClass = selLyrs[0] as IFeatureClass;
                (layer as IFeatureLayer).FeatureClass = fClass;
                IFeatureWorkspace _pFeatWs = selLyrs[0].Workspace as IFeatureWorkspace;
                if (_pFeatWs != null)
                {
                    List<IFeatureLayer> pFeatureLayers = MapHelper.GetAllLayer<IFeatureLayer>((this._Application.Document as IMxDocument).ActiveView.FocusMap);
                    foreach (IFeatureLayer pFeatureLayer in pFeatureLayers)
                    {
                        if (pFeatureLayer.FeatureClass == null)
                        {
                            try
                            {
                                pFeatureLayer.FeatureClass = _pFeatWs.OpenFeatureClass(pFeatureLayer.Name);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLog(ex);
                            }
                        }
                    }
                    (this._Application.Document as IMxDocument).ActiveView.Refresh();
                    (this._Application as IFramework).TOCControl.Update();
                    MsgHelper.ShowInfomationMsgbox("修复完毕");
                }
                else
                {
                    MsgHelper.ShowInfomationMsgbox("请选中矢量要素类");
                }
            }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (_Application == null) return false;
                return ((_Application as IFramework).TOCControl.CustomProperty is ILayer) ? true : false;
            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;
                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
}
